package main

import (
	"encoding/json"
	"fmt"
	"os"
)

const filePath = "./config.json"

type Config struct {
	Name    string /* 配置别名 */
	Home    string /* 工作目录 */
	Workers int32  /* 工作单元数 */
}

func main() {
	p := Config{
		Name:    "主配置",
		Home:    "/home",
		Workers: 16,
	}
	marshal, err := json.Marshal(p)
	if err != nil {
		fmt.Printf("json marshal err: %v", err)
		return
	}
	err = os.WriteFile(filePath, marshal, 0644)
	if err != nil {
		fmt.Printf("write file err: %v", err)
		return
	}
	data, err := os.ReadFile(filePath)
	if err != nil {
		fmt.Printf("read file err :%v", err)
		return
	}
	rp := Config{}
	err = json.Unmarshal(data, &rp)
	if err != nil {
		fmt.Printf("json Unmarshal err: %v", err)
		return
	}
	fmt.Printf("config is: {Name: %v, Home: %v, Workers: %v}", rp.Name, rp.Home, rp.Workers)
}